package qldc.pm.tiensu.service;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.ClassLoaderObjectInputStream;
import com.liferay.portal.kernel.util.PropsUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.model.BaseModel;

import qldc.pm.tiensu.model.BienBanBanGiaoHoSoClp;
import qldc.pm.tiensu.model.CongViecBanGiaoClp;
import qldc.pm.tiensu.model.DoiTuongTienSuClp;
import qldc.pm.tiensu.model.KetQuaTrichLucClp;
import qldc.pm.tiensu.model.LichSuXoaAnTichClp;
import qldc.pm.tiensu.model.PhieuThongTinVeNguoiCoTienSuClp;
import qldc.pm.tiensu.model.TEMP_DoiTuongTienSuClp;
import qldc.pm.tiensu.model.TEMP_PhieuThongTinVeNguoiCoTienSuClp;
import qldc.pm.tiensu.model.TEMP_TienSuClp;
import qldc.pm.tiensu.model.TangThuClp;
import qldc.pm.tiensu.model.TienSuClp;
import qldc.pm.tiensu.model.YeuCauTrichLucClp;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import java.lang.reflect.Method;

import java.util.ArrayList;
import java.util.List;


public class ClpSerializer {
    private static Log _log = LogFactoryUtil.getLog(ClpSerializer.class);
    private static String _servletContextName;
    private static boolean _useReflectionToTranslateThrowable = true;

    public static String getServletContextName() {
        if (Validator.isNotNull(_servletContextName)) {
            return _servletContextName;
        }

        synchronized (ClpSerializer.class) {
            if (Validator.isNotNull(_servletContextName)) {
                return _servletContextName;
            }

            try {
                ClassLoader classLoader = ClpSerializer.class.getClassLoader();

                Class<?> portletPropsClass = classLoader.loadClass(
                        "com.liferay.util.portlet.PortletProps");

                Method getMethod = portletPropsClass.getMethod("get",
                        new Class<?>[] { String.class });

                String portletPropsServletContextName = (String) getMethod.invoke(null,
                        "tiensu-portlet-deployment-context");

                if (Validator.isNotNull(portletPropsServletContextName)) {
                    _servletContextName = portletPropsServletContextName;
                }
            } catch (Throwable t) {
                if (_log.isInfoEnabled()) {
                    _log.info(
                        "Unable to locate deployment context from portlet properties");
                }
            }

            if (Validator.isNull(_servletContextName)) {
                try {
                    String propsUtilServletContextName = PropsUtil.get(
                            "tiensu-portlet-deployment-context");

                    if (Validator.isNotNull(propsUtilServletContextName)) {
                        _servletContextName = propsUtilServletContextName;
                    }
                } catch (Throwable t) {
                    if (_log.isInfoEnabled()) {
                        _log.info(
                            "Unable to locate deployment context from portal properties");
                    }
                }
            }

            if (Validator.isNull(_servletContextName)) {
                _servletContextName = "tiensu-portlet";
            }

            return _servletContextName;
        }
    }

    public static Object translateInput(BaseModel<?> oldModel) {
        Class<?> oldModelClass = oldModel.getClass();

        String oldModelClassName = oldModelClass.getName();

        if (oldModelClassName.equals(BienBanBanGiaoHoSoClp.class.getName())) {
            return translateInputBienBanBanGiaoHoSo(oldModel);
        }

        if (oldModelClassName.equals(CongViecBanGiaoClp.class.getName())) {
            return translateInputCongViecBanGiao(oldModel);
        }

        if (oldModelClassName.equals(DoiTuongTienSuClp.class.getName())) {
            return translateInputDoiTuongTienSu(oldModel);
        }

        if (oldModelClassName.equals(KetQuaTrichLucClp.class.getName())) {
            return translateInputKetQuaTrichLuc(oldModel);
        }

        if (oldModelClassName.equals(LichSuXoaAnTichClp.class.getName())) {
            return translateInputLichSuXoaAnTich(oldModel);
        }

        if (oldModelClassName.equals(
                    PhieuThongTinVeNguoiCoTienSuClp.class.getName())) {
            return translateInputPhieuThongTinVeNguoiCoTienSu(oldModel);
        }

        if (oldModelClassName.equals(TangThuClp.class.getName())) {
            return translateInputTangThu(oldModel);
        }

        if (oldModelClassName.equals(TEMP_DoiTuongTienSuClp.class.getName())) {
            return translateInputTEMP_DoiTuongTienSu(oldModel);
        }

        if (oldModelClassName.equals(
                    TEMP_PhieuThongTinVeNguoiCoTienSuClp.class.getName())) {
            return translateInputTEMP_PhieuThongTinVeNguoiCoTienSu(oldModel);
        }

        if (oldModelClassName.equals(TEMP_TienSuClp.class.getName())) {
            return translateInputTEMP_TienSu(oldModel);
        }

        if (oldModelClassName.equals(TienSuClp.class.getName())) {
            return translateInputTienSu(oldModel);
        }

        if (oldModelClassName.equals(YeuCauTrichLucClp.class.getName())) {
            return translateInputYeuCauTrichLuc(oldModel);
        }

        return oldModel;
    }

    public static Object translateInput(List<Object> oldList) {
        List<Object> newList = new ArrayList<Object>(oldList.size());

        for (int i = 0; i < oldList.size(); i++) {
            Object curObj = oldList.get(i);

            newList.add(translateInput(curObj));
        }

        return newList;
    }

    public static Object translateInputBienBanBanGiaoHoSo(BaseModel<?> oldModel) {
        BienBanBanGiaoHoSoClp oldClpModel = (BienBanBanGiaoHoSoClp) oldModel;

        BaseModel<?> newModel = oldClpModel.getBienBanBanGiaoHoSoRemoteModel();

        newModel.setModelAttributes(oldClpModel.getModelAttributes());

        return newModel;
    }

    public static Object translateInputCongViecBanGiao(BaseModel<?> oldModel) {
        CongViecBanGiaoClp oldClpModel = (CongViecBanGiaoClp) oldModel;

        BaseModel<?> newModel = oldClpModel.getCongViecBanGiaoRemoteModel();

        newModel.setModelAttributes(oldClpModel.getModelAttributes());

        return newModel;
    }

    public static Object translateInputDoiTuongTienSu(BaseModel<?> oldModel) {
        DoiTuongTienSuClp oldClpModel = (DoiTuongTienSuClp) oldModel;

        BaseModel<?> newModel = oldClpModel.getDoiTuongTienSuRemoteModel();

        newModel.setModelAttributes(oldClpModel.getModelAttributes());

        return newModel;
    }

    public static Object translateInputKetQuaTrichLuc(BaseModel<?> oldModel) {
        KetQuaTrichLucClp oldClpModel = (KetQuaTrichLucClp) oldModel;

        BaseModel<?> newModel = oldClpModel.getKetQuaTrichLucRemoteModel();

        newModel.setModelAttributes(oldClpModel.getModelAttributes());

        return newModel;
    }

    public static Object translateInputLichSuXoaAnTich(BaseModel<?> oldModel) {
        LichSuXoaAnTichClp oldClpModel = (LichSuXoaAnTichClp) oldModel;

        BaseModel<?> newModel = oldClpModel.getLichSuXoaAnTichRemoteModel();

        newModel.setModelAttributes(oldClpModel.getModelAttributes());

        return newModel;
    }

    public static Object translateInputPhieuThongTinVeNguoiCoTienSu(
        BaseModel<?> oldModel) {
        PhieuThongTinVeNguoiCoTienSuClp oldClpModel = (PhieuThongTinVeNguoiCoTienSuClp) oldModel;

        BaseModel<?> newModel = oldClpModel.getPhieuThongTinVeNguoiCoTienSuRemoteModel();

        newModel.setModelAttributes(oldClpModel.getModelAttributes());

        return newModel;
    }

    public static Object translateInputTangThu(BaseModel<?> oldModel) {
        TangThuClp oldClpModel = (TangThuClp) oldModel;

        BaseModel<?> newModel = oldClpModel.getTangThuRemoteModel();

        newModel.setModelAttributes(oldClpModel.getModelAttributes());

        return newModel;
    }

    public static Object translateInputTEMP_DoiTuongTienSu(
        BaseModel<?> oldModel) {
        TEMP_DoiTuongTienSuClp oldClpModel = (TEMP_DoiTuongTienSuClp) oldModel;

        BaseModel<?> newModel = oldClpModel.getTEMP_DoiTuongTienSuRemoteModel();

        newModel.setModelAttributes(oldClpModel.getModelAttributes());

        return newModel;
    }

    public static Object translateInputTEMP_PhieuThongTinVeNguoiCoTienSu(
        BaseModel<?> oldModel) {
        TEMP_PhieuThongTinVeNguoiCoTienSuClp oldClpModel = (TEMP_PhieuThongTinVeNguoiCoTienSuClp) oldModel;

        BaseModel<?> newModel = oldClpModel.getTEMP_PhieuThongTinVeNguoiCoTienSuRemoteModel();

        newModel.setModelAttributes(oldClpModel.getModelAttributes());

        return newModel;
    }

    public static Object translateInputTEMP_TienSu(BaseModel<?> oldModel) {
        TEMP_TienSuClp oldClpModel = (TEMP_TienSuClp) oldModel;

        BaseModel<?> newModel = oldClpModel.getTEMP_TienSuRemoteModel();

        newModel.setModelAttributes(oldClpModel.getModelAttributes());

        return newModel;
    }

    public static Object translateInputTienSu(BaseModel<?> oldModel) {
        TienSuClp oldClpModel = (TienSuClp) oldModel;

        BaseModel<?> newModel = oldClpModel.getTienSuRemoteModel();

        newModel.setModelAttributes(oldClpModel.getModelAttributes());

        return newModel;
    }

    public static Object translateInputYeuCauTrichLuc(BaseModel<?> oldModel) {
        YeuCauTrichLucClp oldClpModel = (YeuCauTrichLucClp) oldModel;

        BaseModel<?> newModel = oldClpModel.getYeuCauTrichLucRemoteModel();

        newModel.setModelAttributes(oldClpModel.getModelAttributes());

        return newModel;
    }

    public static Object translateInput(Object obj) {
        if (obj instanceof BaseModel<?>) {
            return translateInput((BaseModel<?>) obj);
        } else if (obj instanceof List<?>) {
            return translateInput((List<Object>) obj);
        } else {
            return obj;
        }
    }

    public static Object translateOutput(BaseModel<?> oldModel) {
        Class<?> oldModelClass = oldModel.getClass();

        String oldModelClassName = oldModelClass.getName();

        if (oldModelClassName.equals(
                    "qldc.pm.tiensu.model.impl.BienBanBanGiaoHoSoImpl")) {
            return translateOutputBienBanBanGiaoHoSo(oldModel);
        }

        if (oldModelClassName.equals(
                    "qldc.pm.tiensu.model.impl.CongViecBanGiaoImpl")) {
            return translateOutputCongViecBanGiao(oldModel);
        }

        if (oldModelClassName.equals(
                    "qldc.pm.tiensu.model.impl.DoiTuongTienSuImpl")) {
            return translateOutputDoiTuongTienSu(oldModel);
        }

        if (oldModelClassName.equals(
                    "qldc.pm.tiensu.model.impl.KetQuaTrichLucImpl")) {
            return translateOutputKetQuaTrichLuc(oldModel);
        }

        if (oldModelClassName.equals(
                    "qldc.pm.tiensu.model.impl.LichSuXoaAnTichImpl")) {
            return translateOutputLichSuXoaAnTich(oldModel);
        }

        if (oldModelClassName.equals(
                    "qldc.pm.tiensu.model.impl.PhieuThongTinVeNguoiCoTienSuImpl")) {
            return translateOutputPhieuThongTinVeNguoiCoTienSu(oldModel);
        }

        if (oldModelClassName.equals("qldc.pm.tiensu.model.impl.TangThuImpl")) {
            return translateOutputTangThu(oldModel);
        }

        if (oldModelClassName.equals(
                    "qldc.pm.tiensu.model.impl.TEMP_DoiTuongTienSuImpl")) {
            return translateOutputTEMP_DoiTuongTienSu(oldModel);
        }

        if (oldModelClassName.equals(
                    "qldc.pm.tiensu.model.impl.TEMP_PhieuThongTinVeNguoiCoTienSuImpl")) {
            return translateOutputTEMP_PhieuThongTinVeNguoiCoTienSu(oldModel);
        }

        if (oldModelClassName.equals(
                    "qldc.pm.tiensu.model.impl.TEMP_TienSuImpl")) {
            return translateOutputTEMP_TienSu(oldModel);
        }

        if (oldModelClassName.equals("qldc.pm.tiensu.model.impl.TienSuImpl")) {
            return translateOutputTienSu(oldModel);
        }

        if (oldModelClassName.equals(
                    "qldc.pm.tiensu.model.impl.YeuCauTrichLucImpl")) {
            return translateOutputYeuCauTrichLuc(oldModel);
        }

        return oldModel;
    }

    public static Object translateOutput(List<Object> oldList) {
        List<Object> newList = new ArrayList<Object>(oldList.size());

        for (int i = 0; i < oldList.size(); i++) {
            Object curObj = oldList.get(i);

            newList.add(translateOutput(curObj));
        }

        return newList;
    }

    public static Object translateOutput(Object obj) {
        if (obj instanceof BaseModel<?>) {
            return translateOutput((BaseModel<?>) obj);
        } else if (obj instanceof List<?>) {
            return translateOutput((List<Object>) obj);
        } else {
            return obj;
        }
    }

    public static Throwable translateThrowable(Throwable throwable) {
        if (_useReflectionToTranslateThrowable) {
            try {
                UnsyncByteArrayOutputStream unsyncByteArrayOutputStream = new UnsyncByteArrayOutputStream();
                ObjectOutputStream objectOutputStream = new ObjectOutputStream(unsyncByteArrayOutputStream);

                objectOutputStream.writeObject(throwable);

                objectOutputStream.flush();
                objectOutputStream.close();

                UnsyncByteArrayInputStream unsyncByteArrayInputStream = new UnsyncByteArrayInputStream(unsyncByteArrayOutputStream.unsafeGetByteArray(),
                        0, unsyncByteArrayOutputStream.size());

                Thread currentThread = Thread.currentThread();

                ClassLoader contextClassLoader = currentThread.getContextClassLoader();

                ObjectInputStream objectInputStream = new ClassLoaderObjectInputStream(unsyncByteArrayInputStream,
                        contextClassLoader);

                throwable = (Throwable) objectInputStream.readObject();

                objectInputStream.close();

                return throwable;
            } catch (SecurityException se) {
                if (_log.isInfoEnabled()) {
                    _log.info("Do not use reflection to translate throwable");
                }

                _useReflectionToTranslateThrowable = false;
            } catch (Throwable throwable2) {
                _log.error(throwable2, throwable2);

                return throwable2;
            }
        }

        Class<?> clazz = throwable.getClass();

        String className = clazz.getName();

        if (className.equals(PortalException.class.getName())) {
            return new PortalException();
        }

        if (className.equals(SystemException.class.getName())) {
            return new SystemException();
        }

        if (className.equals("qldc.pm.tiensu.NoSuchBienBanBanGiaoHoSoException")) {
            return new qldc.pm.tiensu.NoSuchBienBanBanGiaoHoSoException();
        }

        if (className.equals("qldc.pm.tiensu.NoSuchCongViecBanGiaoException")) {
            return new qldc.pm.tiensu.NoSuchCongViecBanGiaoException();
        }

        if (className.equals("qldc.pm.tiensu.NoSuchDoiTuongTienSuException")) {
            return new qldc.pm.tiensu.NoSuchDoiTuongTienSuException();
        }

        if (className.equals("qldc.pm.tiensu.NoSuchKetQuaTrichLucException")) {
            return new qldc.pm.tiensu.NoSuchKetQuaTrichLucException();
        }

        if (className.equals("qldc.pm.tiensu.NoSuchLichSuXoaAnTichException")) {
            return new qldc.pm.tiensu.NoSuchLichSuXoaAnTichException();
        }

        if (className.equals(
                    "qldc.pm.tiensu.NoSuchPhieuThongTinVeNguoiCoTienSuException")) {
            return new qldc.pm.tiensu.NoSuchPhieuThongTinVeNguoiCoTienSuException();
        }

        if (className.equals("qldc.pm.tiensu.NoSuchTangThuException")) {
            return new qldc.pm.tiensu.NoSuchTangThuException();
        }

        if (className.equals(
                    "qldc.pm.tiensu.NoSuchTEMP_DoiTuongTienSuException")) {
            return new qldc.pm.tiensu.NoSuchTEMP_DoiTuongTienSuException();
        }

        if (className.equals(
                    "qldc.pm.tiensu.NoSuchTEMP_PhieuThongTinVeNguoiCoTienSuException")) {
            return new qldc.pm.tiensu.NoSuchTEMP_PhieuThongTinVeNguoiCoTienSuException();
        }

        if (className.equals("qldc.pm.tiensu.NoSuchTEMP_TienSuException")) {
            return new qldc.pm.tiensu.NoSuchTEMP_TienSuException();
        }

        if (className.equals("qldc.pm.tiensu.NoSuchTienSuException")) {
            return new qldc.pm.tiensu.NoSuchTienSuException();
        }

        if (className.equals("qldc.pm.tiensu.NoSuchYeuCauTrichLucException")) {
            return new qldc.pm.tiensu.NoSuchYeuCauTrichLucException();
        }

        return throwable;
    }

    public static Object translateOutputBienBanBanGiaoHoSo(
        BaseModel<?> oldModel) {
        BienBanBanGiaoHoSoClp newModel = new BienBanBanGiaoHoSoClp();

        newModel.setModelAttributes(oldModel.getModelAttributes());

        newModel.setBienBanBanGiaoHoSoRemoteModel(oldModel);

        return newModel;
    }

    public static Object translateOutputCongViecBanGiao(BaseModel<?> oldModel) {
        CongViecBanGiaoClp newModel = new CongViecBanGiaoClp();

        newModel.setModelAttributes(oldModel.getModelAttributes());

        newModel.setCongViecBanGiaoRemoteModel(oldModel);

        return newModel;
    }

    public static Object translateOutputDoiTuongTienSu(BaseModel<?> oldModel) {
        DoiTuongTienSuClp newModel = new DoiTuongTienSuClp();

        newModel.setModelAttributes(oldModel.getModelAttributes());

        newModel.setDoiTuongTienSuRemoteModel(oldModel);

        return newModel;
    }

    public static Object translateOutputKetQuaTrichLuc(BaseModel<?> oldModel) {
        KetQuaTrichLucClp newModel = new KetQuaTrichLucClp();

        newModel.setModelAttributes(oldModel.getModelAttributes());

        newModel.setKetQuaTrichLucRemoteModel(oldModel);

        return newModel;
    }

    public static Object translateOutputLichSuXoaAnTich(BaseModel<?> oldModel) {
        LichSuXoaAnTichClp newModel = new LichSuXoaAnTichClp();

        newModel.setModelAttributes(oldModel.getModelAttributes());

        newModel.setLichSuXoaAnTichRemoteModel(oldModel);

        return newModel;
    }

    public static Object translateOutputPhieuThongTinVeNguoiCoTienSu(
        BaseModel<?> oldModel) {
        PhieuThongTinVeNguoiCoTienSuClp newModel = new PhieuThongTinVeNguoiCoTienSuClp();

        newModel.setModelAttributes(oldModel.getModelAttributes());

        newModel.setPhieuThongTinVeNguoiCoTienSuRemoteModel(oldModel);

        return newModel;
    }

    public static Object translateOutputTangThu(BaseModel<?> oldModel) {
        TangThuClp newModel = new TangThuClp();

        newModel.setModelAttributes(oldModel.getModelAttributes());

        newModel.setTangThuRemoteModel(oldModel);

        return newModel;
    }

    public static Object translateOutputTEMP_DoiTuongTienSu(
        BaseModel<?> oldModel) {
        TEMP_DoiTuongTienSuClp newModel = new TEMP_DoiTuongTienSuClp();

        newModel.setModelAttributes(oldModel.getModelAttributes());

        newModel.setTEMP_DoiTuongTienSuRemoteModel(oldModel);

        return newModel;
    }

    public static Object translateOutputTEMP_PhieuThongTinVeNguoiCoTienSu(
        BaseModel<?> oldModel) {
        TEMP_PhieuThongTinVeNguoiCoTienSuClp newModel = new TEMP_PhieuThongTinVeNguoiCoTienSuClp();

        newModel.setModelAttributes(oldModel.getModelAttributes());

        newModel.setTEMP_PhieuThongTinVeNguoiCoTienSuRemoteModel(oldModel);

        return newModel;
    }

    public static Object translateOutputTEMP_TienSu(BaseModel<?> oldModel) {
        TEMP_TienSuClp newModel = new TEMP_TienSuClp();

        newModel.setModelAttributes(oldModel.getModelAttributes());

        newModel.setTEMP_TienSuRemoteModel(oldModel);

        return newModel;
    }

    public static Object translateOutputTienSu(BaseModel<?> oldModel) {
        TienSuClp newModel = new TienSuClp();

        newModel.setModelAttributes(oldModel.getModelAttributes());

        newModel.setTienSuRemoteModel(oldModel);

        return newModel;
    }

    public static Object translateOutputYeuCauTrichLuc(BaseModel<?> oldModel) {
        YeuCauTrichLucClp newModel = new YeuCauTrichLucClp();

        newModel.setModelAttributes(oldModel.getModelAttributes());

        newModel.setYeuCauTrichLucRemoteModel(oldModel);

        return newModel;
    }
}
